//"use strict";
var g_is_webs_connected = false; // 是否成功连接webs
var g_is_register_ok = false; // 是否成功注册
var g_websocket = null; // websocket对象
var g_message_sn = 1; // 消息序号，递增
var g_screen_zoom = 0; // 屏幕缩放值
var g_main_video_div_id = null; // 主视频窗口ID
var g_last_video_div_mouse_time = 0; // 最后检测到div鼠标消息的时间
var g_iframe_offset_x = 0; // iframe偏移量X
var g_iframe_offset_y = 0; // iframe偏移量Y
var g_iframe_innerHeight = 0; // iframe父窗口innerHeight值
var g_main_video_x = 0; // 主视频的坐标X
var g_main_video_y = 0; // 主视频的坐标Y
var g_main_video_width = 0; // 主视频的坐标宽度
var g_main_video_height = 0; // 主视频的坐标高度
var g_list_open_channel_divid = null; // 打开子通道的列表
var g_timerId1 = 0;
var g_timerId2 = 0;

//获取屏幕缩放比例
function getRatio() {
  var ratio = 0;
  var screen = window.screen;
  var ua = navigator.userAgent.toLowerCase();

  if (window.devicePixelRatio !== undefined) {
    ratio = window.devicePixelRatio;
  } else if (~ua.indexOf("msie")) {
    if (screen.deviceXDPI && screen.logicalXDPI) {
      ratio = screen.deviceXDPI / screen.logicalXDPI;
    }
  } else if (
    window.outerWidth !== undefined &&
    window.innerWidth !== undefined
  ) {
    ratio = window.outerWidth / window.innerWidth;
  }

  if (ratio) {
    ratio = Math.round(ratio * 100);
  }
  return ratio;
}

//function getVideoWndowRect(video_div_id) {var v = document.getElementById(video_div_id);var r = v.getBoundingClientRect();var r0 = getRatio();var r1 = 1;var r2 = g_screen_zoom / 100;var b = 8;r1 = r0 / g_screen_zoom;r0 = r0 / 100;if (window.outerHeight === screen.availHeight && window.outerWidth === screen.availWidth)   b = 0;return {left: r.left * r1 + window.screenX * r2 + b,top: r.top * r1 + (window.outerHeight * r2 - window.innerHeight * r1) + window.screenY * r2 - b,width: r.width*r0,height: r.height*r0};}
function getVideoWndowRect(video_div_id) {
  var v = document.getElementById(video_div_id);
  if (v == null) return null;
  var r = v.getBoundingClientRect();
  var r0 = getRatio();
  var r1 = 1;
  var r2 = g_screen_zoom / 100;
  var b = 8;
  var l = 0,
    t = 0;

  r1 = r0 / g_screen_zoom;
  r0 = r0 / 100;
  b = (window.outerWidth * r2 - window.innerWidth * r0) / 2;
  if (
    window.outerHeight >= screen.availHeight &&
    window.outerWidth >= screen.availWidth
  )
    b = 0;

  //console.log("r0=" + r0 + ",r1=" + r1 + ",r2=" + r2+",b="+b);
  //console.log("window.screenX=" + window.screenX);
  //console.log("window.screenY=" + window.screenY);
  //console.log("window.innerHeight=" + window.innerHeight);
  //console.log("window.outerHeight=" + window.outerHeight);
  //console.log("window.innerWidth=" + window.innerWidth);
  //console.log("window.outerWidth=" + window.outerWidth);
  //console.log("screen.availHeight=" + screen.availHeight);
  //console.log("screen.availWidth=" + screen.availWidth);

  //console.log("left=" + r.left + ",top=" + r.top + ",width=" + r.width + ",height=" + r.height);
  //console.log("g_iframe_offset_x=" + g_iframe_offset_x + ",g_iframe_offset_y=" + g_iframe_offset_y + ",g_iframe_innerHeight=" + g_iframe_innerHeight);

  if (g_iframe_offset_x || g_iframe_offset_y) {
    l = (r.left + g_iframe_offset_x) * r0 + window.screenX * r2 + b;
    t =
      (r.top + g_iframe_offset_y) * r0 +
      (window.outerHeight * r2 - g_iframe_innerHeight * r0) +
      window.screenY * r2 -
      b;
  } else {
    l = r.left * r0 + window.screenX * r2 + b;
    t =
      r.top * r0 +
      (window.outerHeight * r2 - window.innerHeight * r0) +
      window.screenY * r2 -
      b;
  }
  //console.log("l=" + l + ",t=" + t);

  return {
    left: l,
    top: t,
    width: r.width * r0,
    height: r.height * r0,
  };
}

// 初始化(仅主会话有效)
function hcvs_init(main_video_div_id) {
  g_main_video_div_id = main_video_div_id;
  g_iframe_offset_x = 0;
  g_iframe_offset_y = 0;
  g_iframe_innerHeight = 0;

  // 表示有嵌套
  if (window.top != window) {
    if (window.parent != window.top) {
      console.log("不支持多层嵌套!");
      return;
    }

    var iframeDom = null;
    if (window.name)
      iframeDom = window.top.document.getElementById(window.name);
    else iframeDom = window.top.document.getElementById("video_iframe");

    if (iframeDom) {
      var rect = iframeDom.getBoundingClientRect();

      g_iframe_offset_x = rect.left;
      g_iframe_offset_y = rect.top;
    }

    g_iframe_innerHeight = window.top.innerHeight;
  }

  document.addEventListener(
    "visibilitychange",
    function () {
      var isHidden = document.hidden;
      if (isHidden) {
        hcvs_show_main_video_window(false);
        show_all_sub_channel(false);
      } else {
        hcvs_show_main_video_window(true);
        show_all_sub_channel(true);
      }
    },
    false,
  );

  if (g_main_video_div_id) {
    var divVideoWnd = document.getElementById(g_main_video_div_id);
    if (divVideoWnd != null) {
      divVideoWnd.onmousedown = function (e) {
        hcvs_set_main_video_window_active();
      };
    }
  }

  g_main_video_x = 0;
  g_main_video_y = 0;
  g_main_video_width = 0;
  g_main_video_height = 0;
}

// 调用APP
function hcvs_call_app(url) {
  var t = document.createElement("iframe");

  t.style.display = "none";
  t.src = url;
  document.body.appendChild(t);
  setTimeout(function () {
    document.body.removeChild(t);
  }, 1000);
}

// 通知webcontrol.exe重启
function hcvs_notify_restart() {
  if (g_is_webs_connected == false) return;
  var cmd = {
    cmd: "restart",
    sn: g_message_sn,
  };
  g_websocket.send(JSON.stringify(cmd));
  g_message_sn++;
}

// 连接webs, is_main=true表示主会话,=false表示分支会话
function hcvs_connect(
  port,
  on_err_callback,
  on_open_callback,
  on_close_callback,
  on_message_callback,
) {
  if (g_websocket != null) return;

  g_websocket = new WebSocket("ws://127.0.0.1:" + port);
  g_websocket.onopen = function () {
    g_is_webs_connected = true;
    webclient_send_init();
    hcvs_set_main_video_rect();
    on_open_callback();
  };

  g_websocket.onclose = function (e) {
    g_is_webs_connected = false;
    hcvs_disconnect();
    on_close_callback(e);
  };

  // 接受到服务器信息
  g_websocket.onmessage = function (e) {
    if (e.data) {
      //console.log("onmessage",e.data);
      var msg = JSON.parse(e.data);

      on_message_callback(e.data);

      if (msg.cmd == "init") on_init_result(msg);
      else if (msg.cmd == "open_channel") on_open_channel(msg);
      else if (msg.cmd == "register") on_register(msg);
    }
  };

  g_websocket.onerror = function (e) {
    hcvs_disconnect();
    hcvs_call_app("WebControl://");
    on_err_callback(e);
  };
}

// 处理webs发回来的注册结果
function on_register(msg) {
  if (msg.result == "success") {
    g_is_register_ok = true;
  } else if (msg.result == "error") {
    g_is_register_ok = false;
  }
}
// 处理webs发回来的初始化结果
function on_init_result(msg) {
  if (msg.result == "success") {
    g_screen_zoom = Number(msg.zoom);
    // 发送视频区域
    hcvs_set_main_video_rect();
    // 显示视频窗口
    hcvs_show_main_video_window(true);
    // 启动6秒一次心跳包
    g_timerId1 = setInterval(hcvs_send_keeponline, 6000);
    // 启动50毫秒检查一下位置
    g_timerId2 = setInterval(on_check_div_pos, 100);
  } else if (msg.result == "error") {
  }
}

function on_check_div_pos() {
  // 所有子通道位置
  if (g_list_open_channel_divid) {
    var i,
      number = g_list_open_channel_divid.length;
    for (i = 0; i < number; i++) {
      if (
        g_list_open_channel_divid[i].channelid != null &&
        g_list_open_channel_divid[i].is_ok == true
      ) {
        hcvs_set_channel_video_rect(g_list_open_channel_divid[i].div, i);
      }
    }
  }

  // 主窗口位置
  hcvs_set_main_video_rect();
}

// 处理打开成功的结果
function on_open_channel(msg) {
  if (msg.result == "success") {
    if (msg.type == "1") {
      set_sub_channel_open_ok(msg.divid);
      hcvs_set_channel_video_rect(msg.divid);
      hcvs_show_channel_video(msg.divid, true);
    }
  } else if (msg.result == "error") {
  }
}

// 断开连接
function hcvs_disconnect() {
  if (g_websocket == null) return;

  g_is_webs_connected = false;
  g_websocket.close();

  if (g_timerId1 != 0) clearInterval(g_timerId1);
  if (g_timerId2 != 0) clearInterval(g_timerId2);
  g_timerId1 = 0;
  g_timerId2 = 0;

  g_websocket = null;
  g_message_sn = 1;
  g_is_webs_connected = false; // 是否成功连接webs
  g_screen_zoom = 0; // 屏幕缩放值
  g_main_video_div_id = null; // 主视频窗口ID
  g_last_video_div_mouse_time = 0; // 最后检测到div鼠标消息的时间
  g_iframe_offset_x = 0; // iframe偏移量X
  g_iframe_offset_y = 0; // iframe偏移量Y
  g_iframe_innerHeight = 0; // iframe父窗口innerHeight值
  g_main_video_x = 0; // 主视频的坐标X
  g_main_video_y = 0; // 主视频的坐标Y
  g_main_video_width = 0; // 主视频的坐标宽度
  g_main_video_height = 0; // 主视频的坐标高度
  g_list_open_channel_divid = null; // 打开子通道的列表
}

// 反初始化
function hcvs_uninit() {
  if (g_is_webs_connected == false) return;

  var cmd = {
    cmd: "uninit",
    sn: g_message_sn,
  };
  g_websocket.send(JSON.stringify(cmd));

  hcvs_disconnect();
}

function webclient_send_init() {
  if (g_is_webs_connected == false) return;

  var cmd = {
    cmd: "init",
    sn: g_message_sn,
    title: window.top.document.title,
  };
  g_websocket.send(JSON.stringify(cmd));
  g_message_sn++;

  // console.log(cmd);
}

// 发送心跳包
function hcvs_send_keeponline() {
  if (g_is_webs_connected == false) return;

  var cmd = {
    cmd: "keeplonline",
    sn: g_message_sn,
  };
  g_websocket.send(JSON.stringify(cmd));
  g_message_sn++;
}

// 处理视频DIV的鼠标消息
function hcvs_do_video_div_mouse_message() {
  if (g_is_webs_connected == false) return;
  var time1 = Date.now();
  if (time1 - g_last_video_div_mouse_time >= 100) {
    g_last_video_div_mouse_time = time1;
    hcvs_set_main_video_rect();
    //console.log("hcvs_do_video_div_mouse_message...");
  }
}

// 发送主视频窗口坐标
function hcvs_set_main_video_rect() {
  if (g_is_webs_connected == false) return;
  if (g_main_video_div_id == null) return;
  if (g_screen_zoom == 0) return;

  var rc = getVideoWndowRect(g_main_video_div_id);
  if (rc) {
    if (
      g_main_video_x != rc.left ||
      g_main_video_y != rc.top ||
      g_main_video_width != rc.width ||
      g_main_video_height != rc.height
    ) {
      g_main_video_x = rc.left;
      g_main_video_y = rc.top;
      g_main_video_width = rc.width;
      g_main_video_height = rc.height;
      var cmd = {
        cmd: "set_main_video_rect",
        sn: g_message_sn,
        video_left: Math.round(rc.left),
        video_top: Math.round(rc.top),
        video_width: Math.round(rc.width),
        video_height: Math.round(rc.height),
      };

      g_websocket.send(JSON.stringify(cmd));
      g_message_sn++;
      // console.log(cmd);
    }
  }
}

// 显示或隐藏主视频窗口
function hcvs_show_main_video_window(is_show) {
  if (g_is_webs_connected == false) return;
  if (g_main_video_div_id == null) return;

  var cmd = {
    cmd: "show_main_video_window",
    sn: g_message_sn,
    show: is_show ? "true" : "false",
  };

  g_websocket.send(JSON.stringify(cmd));
  g_message_sn++;

  // console.log(cmd);
}

// 激活主视频窗口
function hcvs_set_main_video_window_active() {
  if (g_is_webs_connected == false) return;
  if (g_main_video_div_id == null) return;

  var cmd = {
    cmd: "active_main_video_window",
    sn: g_message_sn,
  };

  g_websocket.send(JSON.stringify(cmd));
  g_message_sn++;
}

// 显示或隐藏列表
function hcvs_show_list(is_show) {
  if (g_is_webs_connected == false) return;
  if (g_main_video_div_id == null) return;
  var cmd = {
    cmd: "show_list",
    sn: g_message_sn,
    show: is_show ? "true" : "false",
  };

  g_websocket.send(JSON.stringify(cmd));
  g_message_sn++;
}

// 用户注册
function hcvs_register(ip, port, account, password) {
  if (g_is_webs_connected == false) return;
  var cmd = {
    cmd: "register",
    sn: g_message_sn,
    ip: ip,
    port: port,
    account: account,
    password: password,
  };

  g_websocket.send(JSON.stringify(cmd));
  g_message_sn++;
}

// 用户注销
function hcvs_unregister() {
  if (g_is_webs_connected == false) return;
  var cmd = {
    cmd: "unregister",
    sn: g_message_sn,
  };

  g_websocket.send(JSON.stringify(cmd));
  g_message_sn++;
}

// 设置主视频窗口类型
function hcvs_main_video_window_type(type, number) {
  if (g_is_webs_connected == false) return;
  if (g_main_video_div_id == null) return;
  var cmd = {
    cmd: "main_video_window_type",
    sn: g_message_sn,
    type: type,
    number: number,
  };

  g_websocket.send(JSON.stringify(cmd));
  g_message_sn++;
}

// 在主视频窗口上打开视频通道, stream_type=0:主码流,=1:子码流
function hcvs_open_channel(channelid, stream_type) {
  if (g_is_webs_connected == false) return;
  if (g_main_video_div_id == null) return;
  if (channelid == null) return;
  if (channelid == "") return;

  var cmd = {
    cmd: "open_channel",
    sn: g_message_sn,
    type: "0",
    channelid: channelid,
    stream_type: stream_type,
  };

  g_websocket.send(JSON.stringify(cmd));
  g_message_sn++;
}

// 在主视频窗口关闭监控视频通道
function hcvs_close_channel(channelid) {
  if (g_is_webs_connected == false) return;
  if (g_main_video_div_id == null) return;
  if (channelid == null) return;
  if (channelid == "") return;
  var cmd = {
    cmd: "close_channel",
    sn: g_message_sn,
    channelid: channelid,
  };

  g_websocket.send(JSON.stringify(cmd));
  g_message_sn++;
}

// 云台控制
function hcvs_send_ptz(channelid, ptz_cmd, ptz_param, speed) {
  if (g_is_webs_connected == false) return;
  var cmd = {
    cmd: "ptz_control",
    sn: g_message_sn,
    channelid: channelid,
    ptz_cmd: ptz_cmd,
    ptz_param: ptz_param,
    speed: speed,
  };

  g_websocket.send(JSON.stringify(cmd));
  g_message_sn++;
}

// 获取通道的索引号
function get_sub_channel_index(divid) {
  if (g_list_open_channel_divid == null) return null;

  var i,
    number = g_list_open_channel_divid.length;
  for (i = 0; i < number; i++) {
    if (g_list_open_channel_divid[i].div == divid) {
      return i;
    }
  }
  return null;
}

// 获取通道的DIV的id值
function get_sub_channel_divid(channelid) {
  if (g_list_open_channel_divid == null) return null;

  var i,
    number = g_list_open_channel_divid.length;
  for (i = 0; i < number; i++) {
    //console.log(g_list_open_channel_divid[i].channelid, g_list_open_channel_divid[i].div);
    if (g_list_open_channel_divid[i].channelid == channelid) {
      //console.log("找到div=", g_list_open_channel_divid[i].div);
      return g_list_open_channel_divid[i].div;
    }
  }
  return null;
}

// 设置通道打开成功
function set_sub_channel_open_ok(divid) {
  if (g_list_open_channel_divid == null) return;

  var i,
    number = g_list_open_channel_divid.length;
  for (i = 0; i < number; i++) {
    if (g_list_open_channel_divid[i].div == divid) {
      g_list_open_channel_divid[i].is_ok = true;
      break;
    }
  }
}

// 关闭通道的DIV的id值
function close_sub_channel_divid(divid) {
  if (g_list_open_channel_divid == null) return;

  var i,
    number = g_list_open_channel_divid.length;
  for (i = 0; i < number; i++) {
    //console.log(g_list_open_channel_divid[i].channelid, g_list_open_channel_divid[i].div);
    if (g_list_open_channel_divid[i].div == divid) {
      g_list_open_channel_divid.splice(i, 1);
      break;
    }
  }
}

// 所有子通道显示/隐藏
function show_all_sub_channel(is_show) {
  if (g_list_open_channel_divid == null) return;

  var i,
    number = g_list_open_channel_divid.length;

  for (i = 0; i < number; i++) {
    if (g_list_open_channel_divid[i].div != null) {
      hcvs_show_channel_video(g_list_open_channel_divid[i].div, is_show);
    }
  }
}

// 是否打开了通道
function hcvs_is_open_sub_channel(divid) {
  if (g_list_open_channel_divid == null) return false;

  var i,
    number = g_list_open_channel_divid.length;
  for (i = 0; i < number; i++) {
    if (g_list_open_channel_divid[i].div == divid) {
      return true;
    }
  }
  return false;
}

// 从div上获取打开的通道ID
function hcvs_get_open_channelid_from_div(divid) {
  if (g_list_open_channel_divid == null) return null;

  var i,
    number = g_list_open_channel_divid.length;
  for (i = 0; i < number; i++) {
    if (g_list_open_channel_divid[i].div == divid) {
      return g_list_open_channel_divid[i].channelid;
    }
  }
  return null;
}

// 打开监控视频子通道, stream_type=0:主码流,=1:子码流;divid:在独立的DIV窗口上打开的div id
function hcvs_open_sub_channel(channelid, stream_type, divid) {
  if (g_is_webs_connected == false) return false;
  if (divid == null) return false;
  if (divid == "") return false;
  if (channelid == null) return false;
  if (channelid == "") return false;
  if (hcvs_is_open_sub_channel(divid)) {
    console.log("该通道已经打开!");
    return false;
  }

  /*
    var channelid2 = hcvs_get_open_channelid_from_div(divid);
    if (channelid2 != null)
    {
        console.log("在div("+divid+")上已经打开通道("+channelid2+")了!");
        return;
    }
    */

  var cmd = {
    cmd: "open_channel",
    sn: g_message_sn,
    type: "1",
    divid: divid,
    channelid: channelid,
    stream_type: stream_type,
  };

  g_websocket.send(JSON.stringify(cmd));
  g_message_sn++;

  var open_channel_divid = {
    channelid: channelid,
    div: divid,
    is_ok: false,
    left: 0,
    top: 0,
    width: 0,
    height: 0,
  };

  if (g_list_open_channel_divid == null) {
    g_list_open_channel_divid = new Array(open_channel_divid);
  } else {
    g_list_open_channel_divid.push(open_channel_divid);
  }

  var divVideoWnd = document.getElementById(divid);
  divVideoWnd.onmousedown = function (e) {
    hcvs_set_channel_video_active(divid, true);
  };
  return true;
}

// 关闭监控视频子通道
function hcvs_close_sub_channel(divid) {
  if (g_is_webs_connected == false) return;
  if (divid == null) return;
  if (divid == "") return;
  var cmd = {
    cmd: "close_channel",
    sn: g_message_sn,
    divid: divid,
  };

  g_websocket.send(JSON.stringify(cmd));
  g_message_sn++;

  // 从数组中删除
  close_sub_channel_divid(divid);
}

// 发送通道视频窗口坐标
function hcvs_set_channel_video_rect(divid, i) {
  if (g_is_webs_connected == false) return;
  if (g_list_open_channel_divid == null) return;
  if (divid == null) return;
  if (divid == "") return;
  if (g_screen_zoom == 0) return;

  if (i == null) i = get_sub_channel_index(divid);

  if (
    i != null &&
    g_list_open_channel_divid[i].div != null &&
    g_list_open_channel_divid[i].is_ok
  ) {
    var rc = getVideoWndowRect(g_list_open_channel_divid[i].div);
    if (
      g_list_open_channel_divid[i].left != rc.left ||
      g_list_open_channel_divid[i].top != rc.top ||
      g_list_open_channel_divid[i].width != rc.width ||
      g_list_open_channel_divid[i].height != rc.height
    ) {
      g_list_open_channel_divid[i].left = rc.left;
      g_list_open_channel_divid[i].top = rc.top;
      g_list_open_channel_divid[i].width = rc.width;
      g_list_open_channel_divid[i].height = rc.height;
      var cmd = {
        cmd: "set_channel_video_rect",
        sn: g_message_sn,
        divid: g_list_open_channel_divid[i].div,
        video_left: Math.round(rc.left),
        video_top: Math.round(rc.top),
        video_width: Math.round(rc.width),
        video_height: Math.round(rc.height),
      };

      g_websocket.send(JSON.stringify(cmd));
      g_message_sn++;
      // console.log(cmd);
    }
  }
}

// 显示或隐藏通道视频窗口
function hcvs_show_channel_video(divid, is_show) {
  if (g_is_webs_connected == false) return;
  if (divid == null) return;
  if (divid == "") return;

  var cmd = {
    cmd: "show_channel_video",
    sn: g_message_sn,
    divid: divid,
    show: is_show ? "true" : "false",
  };

  g_websocket.send(JSON.stringify(cmd));
  g_message_sn++;
}

// 激活通道视频窗口
function hcvs_set_channel_video_active(divid) {
  if (g_is_webs_connected == false) return;
  if (divid == null) return;
  if (divid == "") return;

  var cmd = {
    cmd: "active_channel_video",
    sn: g_message_sn,
    divid: divid,
  };

  g_websocket.send(JSON.stringify(cmd));
  g_message_sn++;
}

// 显示或隐藏所有视频窗口标题条
function hcvs_show_all_video_title(is_show) {
  if (g_is_webs_connected == false) return;

  var cmd = {
    cmd: "show_video_title",
    sn: g_message_sn,
    type: "0",
    is_show_video_title: is_show ? "1" : "0",
  };

  g_websocket.send(JSON.stringify(cmd));
  g_message_sn++;
}

// 显示或隐藏主窗口上的通道视频窗口标题条
function hcvs_show_main_video_title(channelid, is_show) {
  if (g_is_webs_connected == false) return;
  if (channelid == null) return;
  if (channelid == "") return;

  var cmd = {
    cmd: "show_video_title",
    sn: g_message_sn,
    type: "1",
    channelid: channelid,
    is_show_video_title: is_show ? "1" : "0",
  };

  g_websocket.send(JSON.stringify(cmd));
  g_message_sn++;
}
// 显示或隐藏通道视频窗口标题条
function hcvs_show_channel_video_title(divid, is_show) {
  if (g_is_webs_connected == false) return;
  if (divid == null) return;
  if (divid == "") return;

  var cmd = {
    cmd: "show_video_title",
    sn: g_message_sn,
    type: "2",
    divid: divid,
    is_show_video_title: is_show ? "1" : "0",
  };

  g_websocket.send(JSON.stringify(cmd));
  g_message_sn++;
}

// 显示或隐藏用户视频窗口标题条
function hcvs_show_user_video_title(userid, is_show) {
  if (g_is_webs_connected == false) return;
  if (userid == null) return;
  if (userid == "") return;

  var cmd = {
    cmd: "show_video_title",
    sn: g_message_sn,
    type: "3",
    userid: userid,
    is_show_video_title: is_show ? "1" : "0",
  };

  g_websocket.send(JSON.stringify(cmd));
  g_message_sn++;
}

// 打开用户视频
function hcvs_open_user_video(userid) {
  if (g_is_webs_connected == false) return;
  if (userid == null) return;
  if (userid == "") return;

  var cmd = {
    cmd: "open_user_channel",
    sn: g_message_sn,
    sessionid: userid,
  };

  g_websocket.send(JSON.stringify(cmd));
  g_message_sn++;
}

// 关闭用户视频
function hcvs_close_user_video(userid) {
  if (g_is_webs_connected == false) return;
  if (userid == null) return;
  if (userid == "") return;

  var cmd = {
    cmd: "close_user_channel",
    sn: g_message_sn,
    sessionid: userid,
  };

  g_websocket.send(JSON.stringify(cmd));
  g_message_sn++;
}

// 开始呼叫,type="audio",或"video"
function hcvs_make_call(userid, type) {
  if (g_is_webs_connected == false) return;
  if (userid == null) return;
  if (userid == "") return;
  if (type == null) return;
  if (type == "") return;

  var cmd = {
    cmd: "user_call",
    sn: g_message_sn,
    type: type,
    sessionid: userid,
  };

  g_websocket.send(JSON.stringify(cmd));
  g_message_sn++;
}

// 挂断呼叫
function hcvs_hand_up() {
  if (g_is_webs_connected == false) return;

  var cmd = {
    cmd: "hand_up",
    sn: g_message_sn,
    sessionid: userid,
  };

  g_websocket.send(JSON.stringify(cmd));
  g_message_sn++;
}

// 接受呼叫
function hcvs_my_accept_call(userid) {
  if (g_is_webs_connected == false) return;
  if (userid == null) return;
  if (userid == "") return;

  var cmd = {
    cmd: "my_accept",
    sn: g_message_sn,
    sessionid: userid,
  };

  g_websocket.send(JSON.stringify(cmd));
  g_message_sn++;
}

// 拒绝呼叫
function hcvs_my_reject_call(userid) {
  if (g_is_webs_connected == false) return;
  if (userid == null) return;
  if (userid == "") return;

  var cmd = {
    cmd: "my_reject",
    sn: g_message_sn,
    sessionid: userid,
  };

  g_websocket.send(JSON.stringify(cmd));
  g_message_sn++;
}
